package com.xpgk.mvc.bchargeDebt;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.productSales.ProductSalesOrder;

@Service(name = BchargeDebtService.serviceName)
public class BchargeDebtService extends  BaseService{
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BchargeDebtService.class);
	public static final String serviceName = "bchargeDebtService";
	
	/**获取顾客欠款信息
	 * 
	 * @param cusIds
	 * @param orgIds
	 * @return
	 */
	public Map<String, Object> getQkOrder(String cusIds, String orgIds) {
		String  msg=ConstantRender.render_success_code;
		Map<String, Object> map = new HashMap<>();
		List<ProductSalesOrder> psoList = ProductSalesOrder.dao.getProductSalesOrders(cusIds, orgIds);
		if (psoList != null && psoList.size() > 0) {
			map.put("psoList", psoList);
		} else {
			msg= "该顾客无欠款订单";
		}
		map.put("msg", msg);
		return map;

	}
	
	
	/**保存收欠款记录
	 * 
	 * @param organIds
	 * @param ca
	 * @return
	 */
	public  String  repayment(String  organIds,String  userIds,CollectArrears  ca)
	{	
		//保存收欠款记录
		ca.set(CollectArrears.column_organIds, organIds)
			.set(CollectArrears.column_userIds, userIds)
			.set(CollectArrears.column_date, new Date());
		if(!ca.save())
		{
			log.debug("保存收欠款记录失败");
			throw new RuntimeException("保存收欠款记录失败!");
		}
		//更新订单欠款信息
		ProductSalesOrder  pso=ProductSalesOrder.dao.findById(ca.getStr(CollectArrears.column_orderIds));
		if(pso!=null)
		{	
			float  shk=pso.getFloat(ProductSalesOrder.column_proceeds)+ca.getFloat(CollectArrears.column_proceeds);
			pso.set(ProductSalesOrder.column_proceeds, shk)
				.set(ProductSalesOrder.column_arrears, ca.getFloat(CollectArrears.column_arrears))
				.set(ProductSalesOrder.column_gxsj, new Date());
			if(!pso.update())
			{
				log.debug("更新订单欠款信息失败");
				throw new RuntimeException("更新订单欠款信息失败!");
			}
		}
		String msg=ConstantRender.render_success_code;
		return  msg;
		
	}
	
}
